%-------------------------------------------------------------------------
%
% dial.tex
%
% A MDP model of multiagent dialogue system 

\documentclass[14pt]{article}
%\documentclass[letterpaper]{aamas2009}
\usepackage{amsfonts}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
%\usepackage{calc}
\usepackage[section]{algorithm}
\usepackage{algorithmic}
\usepackage{latexsym, amssymb}
\usepackage{times}
\usepackage{amsmath}
\usepackage{url}
\usepackage{rotating}

\renewcommand{\topfraction}{0.9}
\renewcommand{\bottomfraction}{0.9}
\newcommand{\tx}[1]{\mbox{#1}}

\newcommand{\fn}[1]{\footnote{\scriptsize #1}}
\newcommand{\acro}[1]{{\textsc{\lowercase{#1}}}}
\newcommand{\note}[1]{\begin{quote}\acro{#1}\end{quote}}

%%%
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\newcommand{\algorithmicprocedure}{\textbf{procedure}\ }
\newcommand{\algorithmicfunction}{\textbf{function}\ }
\newcommand{\algorithmicprocend}{\textbf{end procedure}\ }
\newcommand{\algorithmicfunctionend}{\textbf{end function}\ }
%%%

\newtheorem{definition}{Definition}
\newtheorem{proposition}{Proposition}
\newtheorem{property}{Property}
\newtheorem{theorem}{Theorem}
\newtheorem{proof}{Proof}
%

%----------
\usepackage{listings}
\usepackage{color}
\usepackage{textcomp}
\definecolor{listinggray}{gray}{0.9}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}
\lstset{
	backgroundcolor=\color{lbcolor},
	tabsize=4,
	rulecolor=,
	language=Java,
        basicstyle=\scriptsize,
        upquote=true,
        aboveskip={1.5\baselineskip},
        columns=fixed,
        showstringspaces=false,
        extendedchars=true,
        breaklines=true,
        prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
        frame=single,
        showtabs=false,
        showspaces=false,
        showstringspaces=false,
        identifierstyle=\ttfamily,
        keywordstyle=\color[rgb]{0,0,1},
        commentstyle=\color[rgb]{0.133,0.545,0.133},
        stringstyle=\color[rgb]{0.627,0.126,0.941},
}
%----------

%
\begin{document}

% -------------------------------------------------------------------------


\title{Argumentation-Trust XML Inputs and JAVA APIs}
 \author{ Yuqing Tang}


\maketitle % typeset the title of the contribution

% \begin{abstract}

% \end{abstract}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Introduction}
The system is implemented in JAVA with output processed by graphviz,
dot2tex and latex (with tikz). Our development environment is listed
as the following:
\begin{itemize}
\item Ubuntu 10.10
\item JAVA version\\
  java version "1.6.0\_20"\\
  OpenJDK Runtime Environment (IcedTea6 1.9.7) (6b20-1.9.7-0ubuntu1)\\
  OpenJDK Client VM (build 19.0-b09, mixed mode, sharing)
\item Apache Ant version 1.8.0 compiled on May 9 2010
\item Graphviz version 2.26.3 (20100126.1600)
\item pdfTeX 3.1415926-1.40.10-2.2 (TeX Live 2009/Debian) (with tikz
  package installed)
\item Dot2tex version 2.8.7
\end{itemize}
Please note that any environment that is compatible with the above
versions of ANT, JAVA, graphviz, dot2tex and latex (with tikz) should
be able to build and run this prototype system.

\subsection{Build}
Untar or check out the source files into directory ``<ArgTrust>'', and
execute \lstset{language=bash}
\begin{lstlisting}
  cd <ArgTrust>
%  ant jar
  ant -propertyfile ant.properties jar
\end{lstlisting}

%The source code comes from  a Netbeans project. If you checkout
%a fresh copy of the source code without being processed by Netbeans for at least one time, 
%the command ``ant jar'' might not be able to set the class-path 
%in ``/META-INF/MANIFEST.MF'' properly. In this case, after ``ant jar'' is run, edit
%``/META-INF/MANIFEST.MF'' in ArgTrust.jar  and change it to be
% \begin{lstlisting}
%Manifest-Version: 1.0
%Ant-Version: Apache Ant 1.8.2
%Created-By: 1.6.0_26-b03 (Sun Microsystems Inc.)
%Class-Path: lib/xercesImpl.jar lib/xmlParserAPIs.jar lib/aima-core.jar
%  lib/jgraphx.jar lib/guava-r09.jar lib/jopt-simple-3.3.jar
%X-COMMENT: Main-Class will be added automatically by build
%Main-Class: argtrust.cmd.ArgTrust
%\end{lstlisting}
%and copy the folder "<ArgTrust>/lib" in to "<ArgTrust>/dist''.


\subsection{Run an example}
\lstset{language=bash}
\begin{lstlisting}
  cd /tmp 
  java -jar <ArgTrust>/dist/ArgTrust.jar  -i <ArgTrust>/XML-samples/jcl.xml -o jcl.dot -a john  -q  "Watch(hce)"
  <ArgTrust>/bin/graph-arg-trust.sh jcl.dot
\end{lstlisting}
This will output a file "jcl.pdf" of the trust-argumentation graph of
John's view on the issue of whether to watch the movie "hce"
($\tx{Watch(hce)}$).

The options of the command line are summarized as follows:
\begin{lstlisting}
Usage: argtrust.cmd.ArgTrust [options]
Example: argtrust.cmd.ArgTrust -i <input xml> -o <output file/folder> -a <agent> -q <question1>;<question2>;...
Option (* = required)                   Description                            
---------------------                   -----------                            
-?, -h                                  show help                              
-a <agent>                                                                     
--arg <argumententation semantics name>                                        
--blarg <belief-on-argument semantics                                          
  name>                                                                        
* -i <File: input xml>                                                         
* -o <File: output file/folder>                                                
--ot <output type: dot|dot2tex|html>                                           
-q <question list (separated by ``;''): question1;question2;...>  
--tb <trust-to-belief semantics name:                                          
  : [TidalBelief|CertPropBelief]>                                              
--tr <trust semantics name:                                                    
  [TidalTrust|CertPropTrust]>  
\end{lstlisting}

  

\subsection{A supported sub-language of first-order logic}
A sub-language of first-order logic sentences is used to represent
premises, conclusions and intermediate conclusions in knowledge
representation and argument construction. The syntax of first-order
logic sentences is defined as the following:
\begin{align*}
  Sentence \to&  AtomicSentence | ComplexSentence \\
  AtomicSentence \to& Predicate(Term,...) \\
  ComplexSentence  \to&  (Sentence) \\
  & | \ NOT\ Sentence \\
  &  |\ Sentence\ AND\ Sentence \\
  &  |\ Sentence\ OR\ Sentence \\
  & |\ Sentence\ =>\ Sentence \\% \textrm{ (implies) }\\
  & |\ Sentence\ <=>\ Sentence \\% \textrm{ (bi-conditional) }\\
  Term  \to & Constant \\
  &  | Variable\\
  Constant \to & A | X_1 |John\\
  &|...\textrm{(symbols that are explicitly specified as constants)}\\
  Variable  \to & x | s \\
  & |... \textrm{(symbols that are started with a lower case letter and are not explicitly specified as constants)}
\end{align*}
$=>$ is implication; $< =>$ is bi-conditionals.

The input knowledge is composed of facts ($Fact$) and inference rules
($\tx{InferenceRule}$) defined as below:
\begin{align*}
  Fact \to&  Sentence \\
  InferenceRule \to& \frac{PremiseList}{Conclusion} \\
  PremiseList \to& Sentence, ...\\
  Conclusion \to& Sentence
\end{align*}


\subsection*{Soundness and completeness}

The current implemented algorithms are sound and complete if the
following are literals (either positive atomic sentence or negated
atomic sentence):
\begin{itemize}
\item Facts
\item Premises and conclusions of every inference rule.
\end{itemize}



\section{XML input}
\lstdefinelanguage{XML} { morestring=[b]", morestring=[s]{>}{<},
  morecomment=[s]{<?}{?>}, stringstyle=\color{black},
  identifierstyle=\color{darkblue}, keywordstyle=\color{cyan},
  morekeywords={xmlns,version,type}% list your attributes here
}
\lstset{language=XML}
Expressed the beliefs with the above FOL-language, we can write the
trust and belief base in an XML file. The argumentation-trust base 
is enclosed in 
 \begin{lstlisting}
    <argtrust>
        <domain>
        ...
        </domain>
        <trustnet>
        ...
        </trustnet>
        <beliefbase>
        ...
        </beliefbase>
    </argtrust>
 \end{lstlisting}
and composed of the following elements:
\begin{enumerate}
\item Create a FOL domain
 \begin{lstlisting}
     <domain>
        <constant> ... </constant>
        ...
        <predicate> ... </predicate>
        ...
    </domain>
 \end{lstlisting}
  \begin{itemize}
  \item Add constants
    \begin{lstlisting}
        <constant> hce   </constant>
        ...
     \end{lstlisting}
  \item Add predicates
    \begin{lstlisting}
    <predicate> SpanishFilm </predicate>
    ...
    \end{lstlisting}
  \end{itemize}

\item Create trust network
  \begin{lstlisting}
  <trustnet>
       <agent> john </agent>
       ...
        <trust>
            <truster> john </truster>
            <trustee> mary </trustee>
            <level> 0.9 </level>
        </trust>
       ...
  </trustnet>
  \end{lstlisting}
  \begin{itemize}
  \item Add agents
    \begin{lstlisting}
        <agent> alice </agent>
        ...
    \end{lstlisting}

  \item Add trust links
    \begin{lstlisting}
        <trust>
            <truster> john </truster>
            <trustee> mary </trustee>
            <level> 0.9 </level>
        </trust>
       ...
    \end{lstlisting}
    Two types of trust link measurement are supported (however, in a single XML input only one type of measurement is allowed)
    \begin{itemize}
    \item Levels
   \begin{lstlisting}
        <trust>
            <truster> john </truster>
            <trustee> mary </trustee>
            <level> 0.9 </level>
        </trust>
       ...
    \end{lstlisting}
    \item CertPropMeasure
   \begin{lstlisting}
         <trust>
            <truster> john </truster>
            <trustee> mary </trustee>
            <certprop>
               <r> 5 </r>
               <s> 7 </s>
            </certprop>
        </trust>
    \end{lstlisting}
    where $r$ is the number of positive evidences, and $s$ is the number of negative evidences.
    \end{itemize}
  \end{itemize}


\item Create a belief base
  \begin{lstlisting}
    <beliefbase>
        <belief>
            <agent> john </agent>
            <fact> IndieFilm(hce) </fact>
            <level> 0.8 </level>
        </belief>
        ...
        <belief>
            <agent> dave </agent>
            <rule>
                <premise> IndieFilm(x) </premise>
                <premise> DirectedBy(x, almodovar) </premise>
                <conclusion> Watch(x) </conclusion>
            </rule>
            <level> 0.89 </level>
        </belief>
       ...
    </beliefbase>
  \end{lstlisting}
  \begin{itemize}
  \item Add believed facts
    \begin{lstlisting}
        <belief>
            <agent> john </agent>
            <fact> IndieFilm(hce) </fact>
            <level> 0.8 </level>
        </belief>
        ...
    \end{lstlisting}

  \item Add believed inference rules
    \begin{lstlisting}
        <belief>
            <agent> dave </agent>
            <rule>
                <premise> IndieFilm(x) </premise>
                <premise> DirectedBy(x, almodovar) </premise>
                <conclusion> Watch(x) </conclusion>
            </rule>
            <level> 0.89 </level>
        </belief>
       ...
    \end{lstlisting}
  \item Add customized defeat rules
 \begin{lstlisting}
        <belief>
            <agent> john </agent>
            <defeatrule direction=''bi''>
                <defeater> IndieFilm(x) </defeater>
                <defeatee> DirectedBy(x, almodovar) </defeatee>
            </defeatrule>
            <level> 0.89 </level>
        </belief>
       ...
    \end{lstlisting}
   where attribute $\tx{direction}$ is optional, when it has a value of ``bi'' a bi-directional defeat is added. Currently 
   the belief level is ignored by the implementation but reserved for future usage.
  \item     Two types of belief measurement are supported (however, in a single XML input only one type of measurement is allowed)
    \begin{itemize}
    \item Levels
      \begin{lstlisting}
        <belief> <agent> john </agent> <fact> IndieFilm(hce) </fact>
        <level> 0.9 </level> </belief> ...
      \end{lstlisting}
    \item CertPropMeasure
   \begin{lstlisting}
         <belief>
            <agent> john </agent>
            <fact> IndieFilm(hce) </fact>
            <certprop>
               <r> 5 </r>
               <s> 7 </s>
            </certprop>
        </belief>
    \end{lstlisting}
    where $r$ is the number of positive evidences, and $s$ is the number of negative evidences.
    \end{itemize}
  \end{itemize}

\item Create query
   \begin{lstlisting}
     <query>
        <agent> john </agent>
        <!-- multiple questions are allowed -->
        <question> Watch(hce) </question>
        <question> IndieFilm(hce) </question>
     </query>
   \end{lstlisting}
  \end{enumerate}

\subsection{A full XML example}
  An example of a full xml formatting trust/belief base in level measurement is
 as follows: \lstinputlisting[language=XML]{jcl.xml}
The above xml produces Figure~\ref{fig:level-xml}.
\begin{sidewaysfigure}
     \includegraphics[width=0.8\paperheight]{./jcl}%
\caption{An arg-trust graph with level measurement}
\label{fig:level-xml}
\end{sidewaysfigure}


  An example of a full xml formatting trust/belief base in CertProp measurement is
 as follows: \lstinputlisting[language=XML]{jcl-certprop.xml}
The above xml produces Figure~\ref{fig:certprop-xml}.
\begin{sidewaysfigure}
     \includegraphics[width=.8\paperwidth]{./jcl-certprop}%
\caption{An arg-trust graph with CertProp measurement}
\label{fig:certprop-xml}
\end{sidewaysfigure}


\section{Argumentation-Trust Steps in JAVA}
\lstset{language=Java}
\begin{enumerate}
\item Create a first-order language domain for the parser
  \begin{lstlisting}
    FOLDomain domain = new FOLDomain();
  \end{lstlisting}

  \begin{itemize}
  \item Add constants
    \begin{lstlisting}
      domain.addConstant("hce"); ...
    \end{lstlisting}
  \item Add predicates
    \begin{lstlisting}
      domain.addPredicate("IndieFilm"); ...
    \end{lstlisting}

  \end{itemize}
\item Create trust network
  \begin{lstlisting}
    TrustNet trust_net = new TrustNet();
  \end{lstlisting}
  \begin{itemize}
  \item Add agents
    \begin{lstlisting}
      Agent john = trust_net.getAgent("john"); Agent alice =
      trust_net.getAgent("alice"); ...
    \end{lstlisting}

  \item Add trust links
    \begin{lstlisting}
      trust_net.trust(john, mary, 0.9); trust_net.trust(mary, jane,
      0.7); ...
    \end{lstlisting}
  \end{itemize}

\item Create a FOL parser
  \begin{lstlisting}
    FOLParser fol = new FOLParser(domain);
  \end{lstlisting}

\item Create a belief base
  \begin{lstlisting}
    BeliefBase bb = new BeliefBase();
  \end{lstlisting}
  \begin{itemize}
  \item Add believed facts
    \begin{lstlisting}
      bb.believe(john, fol.parse("IndieFilm(hce)"), 1);
      bb.believe(john, fol.parse("SpanishFilm(hce)"), 0.9); ...
    \end{lstlisting}

  \item Add believed inference rules
    \begin{lstlisting}
      bb.believe(jane, new InferenceRule()
      .P_(fol.parse("IndieFilm(x)")) .P_(fol.parse("SpanishFilm(x)"))
      .C_(fol.parse("NOT Watch(x)")), 1);

      bb.believe(dave, new InferenceRule()
      .P_(fol.parse("IndieFilm(x)")) .P_(fol.parse("DirectedBy(x,
      almodovar)")) .C_(fol.parse("Watch(x)")), 1); ...
    \end{lstlisting}
    Please note that the premise-adding and conclusion-setting methods
    of $InferenceRule$ return the rule itself for the compactness of
    the code:
    \begin{lstlisting}
      public class InferenceRule { ...  public InferenceRule
        P_(Sentence premise) { this.addPremise(premise); return this;
        }

        public InferenceRule C_(Sentence conclusion) { mConclusion =
          conclusion; return this; } }
    \end{lstlisting}
  \end{itemize}

\item Create argumentation system
  \begin{itemize}
  \item Extract beliefs into a knowledge base
    \begin{lstlisting}
      FOLKnowledgeBase kb = new FOLKnowledgeBase();
      kb.acceptKnowledgeBase(bb.getFactSet(), bb.getRuleSet());
    \end{lstlisting}

  \item Create an argumentation system based on the knowledge base
    \begin{lstlisting}
      ArgSys arg_sys = new ArgSys(kb);
    \end{lstlisting}

  \item Create an argumentation graph for a given conclusion, and
    return an ArguingNode for the root of the graph
    \begin{lstlisting}
      ArguingNode watch_hce_arggraph =
      arg_sys.createArgGraph(fol.parse("Watch(hce)"));
    \end{lstlisting}
  \end{itemize}

\item Export the argumentation graph into a Graphviz dot file
  \begin{itemize}
  \item Create the graphviz output
    \begin{lstlisting}
      ArgTrustGraph gv = new ArgTrustGraph();

    \end{lstlisting}

  \item Tie the graphviz output with the trust net, the belief base
    and the argumentation system
    \begin{lstlisting}
      gv.setArgSys(arg_sys); gv.setBeliefBase(bb);
      gv.setTrustNet(trust_net);
    \end{lstlisting}

  \item Open an output file
    \begin{lstlisting}
      gv.openFile("/home/tang/temp/jcl.dot");
    \end{lstlisting}

  \item Draw the graph
    \begin{lstlisting}
      gv.draw(watch_hce_arggraph, trust_net.getTrustNode(john)); //
      gv.draw(arg_sys.getAllNodes(), trust_net.getTrustNodes());
    \end{lstlisting}

  \item Close the graphviz
    \begin{lstlisting}
      gv.close();
    \end{lstlisting}

  \end{itemize}

\item Produce PDF output
  \begin{lstlisting}
    dot -Tpdf -O jcl.dot
  \end{lstlisting}

\end{enumerate}

\subsection{A full JAVA example}
\begin{lstlisting}
  public static void main(String[] args) { //Create FOL domain:
    FOLDomain domain = new FOLDomain(); domain.addConstant("hce");
    domain.addConstant("almodovar"); domain.addPredicate("IndieFilm");
    domain.addPredicate("SpanishFilm");
    domain.addPredicate("DirectedBy"); domain.addPredicate("Comedy");
    domain.addPredicate("Watch");

    //TrustNetwork: TrustNet trust_net = new TrustNet(); //Agent:
    Agent john = trust_net.getAgent("john"); Agent alice =
    trust_net.getAgent("alice"); Agent dave =
    trust_net.getAgent("dave"); Agent jane =
    trust_net.getAgent("jane"); Agent mary =
    trust_net.getAgent("mary"); //Trusts: trust_net.trust(john, mary,
    0.9); trust_net.trust(mary, jane, 0.7); trust_net.trust(mary,
    dave, 0.8); trust_net.trust(alice, dave, 0.4);
    trust_net.trust(alice, jane, 0.6);


    //Create FOL sentences and inference rules FOLParser fol = new
    FOLParser(domain);


    //Belief base: BeliefBase bb = new BeliefBase(); bb.believe(john,
    fol.parse("IndieFilm(hce)"), 1); bb.believe(john,
    fol.parse("SpanishFilm(hce)"), 1); bb.believe(john,
    fol.parse("DirectedBy(hce, almodovar)"), 1);

    bb.believe(john, (new InferenceRule()) .P_(fol.parse("Comedy(x)"))
    .C_(fol.parse("NOT Watch(x)")), 1);

    bb.believe(jane, (new InferenceRule())
    .P_(fol.parse("IndieFilm(x)")) .P_(fol.parse("SpanishFilm(x)"))
    .C_(fol.parse("NOT Watch(x)")), 1);

    bb.believe(dave, (new InferenceRule())
    .P_(fol.parse("IndieFilm(x)")) .P_(fol.parse("DirectedBy(x,
    almodovar)")) .C_(fol.parse("Watch(x)")), 1);

    bb.believe(john, new InferenceRule() .P_(fol.parse("DirectedBy(x,
    almodovar)")) .C_(fol.parse("NOT IndieFilm(x)")), 1);
    bb.believe(mary, fol.parse("IndieFilm(hce)"), 1);

    //Argumentation system: FOLKnowledgeBase kb = new
    FOLKnowledgeBase(); kb.acceptKnowledgeBase(bb.getFactSet(),
    bb.getRuleSet()); ArgSys arg_sys = new ArgSys(kb); ArguingNode
    watch_hce_arggraph =
    arg_sys.createArgGraph(fol.parse("Watch(hce)"));

    watch_hce_arggraph.debug_print(System.out);

    ArgTrustGraph gv = new ArgTrustGraph(); gv.setArgSys(arg_sys);
    gv.setBeliefBase(bb); gv.setTrustNet(trust_net); //output a dot
    file to /tmp/jcl.dot gv.openFile("/tmp/jcl.dot"); //Draw the view
    of john on watch_hce gv.draw(watch_hce_arggraph,
    trust_net.getTrustNode(john)); //Draw the views of all the agents
    on all generated arguments // gv.draw(arg_sys.getAllNodes(),
    trust_net.getTrustNodes()); gv.close();

  }
\end{lstlisting}

\section{Integrate with trust, belief and argumentation status}
\begin{enumerate}
\item Implement a trust semantics 
 \begin{lstlisting}        
public interface ITrustSemantics {

    public Measure trust(Agent agent, Agent other);

    public IMeasureOperators getOperators();
}
 \end{lstlisting}
For example, 
 \begin{lstlisting}        
        ITrustSemantics trust_sem = new TidalTrust(trust_net, 0);
        argtrust_graph.setTrustSemantics(trust_sem);
 \end{lstlisting}

\item Implement a trust-to-belief semantics
 \begin{lstlisting}        
public interface ITrustBeliefSemantics {
    public Measure believe(Agent agent, Object obj);
    public IMeasureOperators getOperators();
}
 \end{lstlisting}
For example, 
 \begin{lstlisting}        
        ITrustBeliefSemantics trust_belief_sem = new TidalTrustBelief(bb, trust_sem);
        argtrust_graph.setTrustBeliefSemantics(trust_belief_sem);
 \end{lstlisting}

\item Implement a belief-to-argument combination belief semantics
 \begin{lstlisting}        
public interface IArgumentBeliefSemantics {
    public Measure believe(Agent agent, Argument argument);
}
 \end{lstlisting}
 For example, 
\begin{lstlisting}        
        IArgumentBeliefSemantics arg_belief_sem = new MinArgumentBelief(trust_belief_sem);
        argtrust_graph.setArgumentBeliefSemantics(arg_belief_sem);
 \end{lstlisting}

\item Implement an argumentation semantics
 \begin{lstlisting}        
public interface IArgumentationSemantics {

    public ArgumentStatus getStatus(Agent agent, Argument arg);

    public boolean isEffective(Agent agent, Defeat defeat);
}
 \end{lstlisting}
 For example, 
 \begin{lstlisting}        
        IArgumentationSemantics argumentation_sem = new LabelSemantics(arg_sys, arg_belief_sem);
        argtrust_graph.setArgumentationSemantics(argumentation_sem);
 \end{lstlisting}
 
\end{enumerate}


% -------------------------------------------------------------------------

\begin{bibliography}{../bibtex-lib/arg-dial-planning,../bibtex-lib/tang-agents,simon}
  \bibliographystyle{abbrv}

\end{bibliography}
\end{document}

%-------------------------------------------------------------------------

